<template>
<div class="rightDrawerWithoutCol">
  <div class="searchBtns" style="padding: 10px">
      <auth-button type="primary" :auth="$managePerms.adasEcuConfig.view" @click="refreshData(true)">
        <i class="iconfont icon-chazhao"></i>
        {{ $t('inquiry') }}
      </auth-button>
      <a-button @click="batchSwitch(true, 'batchOpenLoading')" :loading="batchOpenLoading"
                :disabled="!$hasPermission($managePerms.adasEcuConfig.operate)">批量开启</a-button>
      <a-button @click="batchSwitch(false, 'batchCloseLoading')" :loading="batchCloseLoading"
                :disabled="!$hasPermission($managePerms.adasEcuConfig.operate)">批量取消</a-button>
  </div>
  <s-table ref="table"
           style="height: calc(100% - 60px)"
           size="default"
           rowKey="id"
           :columns="cols"
           :data="loadData"
           :showPagination="false"
           :pageSize="1000"
           :addLoading="true"
           :row-selection="{ selections:true,selectedRowKeys:selectedRowKeys, onChange: onTableSelectChange }"
           :getPopupContainer="$commonFun.getPopupContainer">
    <template #ecuIDriveUrlLimit="text,record">
      <a-switch :checked="text === 1" @change="(e) => switchModelEcu(e, record)" :loading="record.loading"
                :disabled="!$hasPermission($managePerms.adasEcuConfig.operate)"/>
    </template>
  </s-table>
</div>
</template>
<script>
  import i18n from "@/lang";
  import api from "@api/modules/system/adasEcuConfig";

  export default {
    name: 'adasEcuConfigList',
    props: {
      modelId : {
        type: String,
        required: true
      },
      updateCallback: {
        type: Function,
        default: () => {}
      }
    },
    data() {
      return {
        changedEcuConfigs: [],
        selectedRowKeys: [],
        batchOpenLoading: false,
        batchCloseLoading: false,
        cols: [
          {
            title: i18n.t('ECU 名称'),
            dataIndex: 'ecuName',
            width: 230,
          },
          {
            title: i18n.t('ECU ID'),
            dataIndex: 'ecuModuleId',
            width: 230,
          },
          {
            title: i18n.t('软件编码'),
            dataIndex: 'softwareCode',
            width: 230,
          },
          {
            title: i18n.t('校验开关'),
            dataIndex: 'modelEcuIDriveUrlLimit',
            width: 130,
            scopedSlots: { customRender: 'ecuIDriveUrlLimit' }
          },
        ]
      }
    },
    methods: {
      loadData(page) {
        if (!this.modelId) {
          return null;
        }
        return api.getEcuConfig({...page, id: this.modelId});
      },
      refreshData(isToFirstPage = false) {
        const vm = this
        vm.$nextTick(() => {
          this.$refs.table.refresh(isToFirstPage)
        })
      },
      switchModelEcu(e, record) {
        let vm = this;
        let target = vm.changedEcuConfigs.find(item => item.id === record.id);
        let enabled = e;
        // let enabled = e.target.checked;
        if (target) {
          target.modelEcuIDriveUrlLimit = enabled ? 1 : 0;
        } else {
          vm.changedEcuConfigs.push({
            id: record.id,
            modelEcuIDriveUrlLimit: enabled ? 1 : 0
          })
        }
        this.$confirm({
          title: vm.$t('车型ECU配置'),
          content: enabled ? '您确定要开启吗？开启后，配置的车辆将会在下载该ECU升级文件前校验下载地址的域名'
            : '您确定要关闭校验吗？关闭后，车辆在下载该ECU升级文件前不再校验下载地址的域名',
          onOk() {
            record.loading=true;
            api.saveModelEcuConfig(record.modelId, [{
                id: record.id,
                modelEcuIDriveUrlLimit: enabled ? 1 : 0
              }]).then(() => {
              vm.refreshData(false);
              vm.updateCallback();
            }).finally(() => record.loading = false);
          },
          onCancel() {
          }
        })
      },
      batchSwitch(enabled, loading) {
        let vm = this;
        this.$confirm({
          title: vm.$t('车型ECU配置'),
          content: enabled ? '您确定要开启选中ECU的校验吗？开启后，配置的车辆将会在下载该ECU升级文件前校验下载地址的域名' :
            '您确定要关闭选中ECU的校验吗？关闭后，车辆在下载该ECU升级文件前不再校验下载地址的域名',
          onOk() {
            vm[loading] = true;
            api.saveModelEcuConfig(vm.modelId, vm.selectedRowKeys.map(value => {
              return {
                id: value,
                modelEcuIDriveUrlLimit: enabled ? 1 : 0
              }
            })).then(() => {
              vm.selectedRowKeys=[];
              vm.refreshData(false);
              vm.updateCallback();
            }).finally(() => vm[loading] = false);
          },
          onCancel() {
          }
        })
      },
      onTableSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys;
      },
    }
  }
</script>